package com.yanqu.road.web.controller.game.forbid;

import com.yanqu.road.dao.impl.player.ChatControlDaoImpl;
import com.yanqu.road.entity.admin.AdminResult;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.enums.eMessageType;
import com.yanqu.road.entity.enums.ePlatformForbiddenType;
import com.yanqu.road.entity.message.BanChatUserData;
import com.yanqu.road.entity.platform.PlatformForbidden;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.bussiness.servercenter.PlatformUserIdInfoBussiness;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.ChatControlMgr;
import com.yanqu.road.server.manger.ForbiddenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yanqu.road.entity.enums.ePlatformForbiddenType.forName;

/**
 * @Author : Cxp
 * @Time : 2025-02-17
 * @Description : 封禁父类
 */

public abstract class BanBase {
    protected long userId;
    protected long endTime;
    protected static ePlatformForbiddenType type;
    protected Map<String, List<String>> mailMap; // language -> (title,content)

    public BanBase(long userId, long endTime) {
        this.userId = userId;
        this.endTime = endTime;
    }

    abstract AdminResult banAction(); // 封禁

    abstract AdminResult unbanAction(); // 解禁

    public AdminResult ban() {
        return banAction();
    }

    public AdminResult unban() {
        return unbanAction();
    }

    /**
     * 屏蔽聊天
     *
     * @param userId    用户ID
     * @param serverId  区服ID
     * @param channelId 渠道ID
     * @param endTime   封禁结束时间
     * @return AdminResult
     */
    public static AdminResult clearChat(long userId, long serverId, int channelId, long endTime) {
        Map<Long, Map<Integer, BanChatUserData>> banChatUserDataMap = ChatControlMgr.getBanChatUserDataMap();
        if (!banChatUserDataMap.containsKey(userId) || !banChatUserDataMap.get(userId).containsKey(type.getType())) {
            BanChatUserData data = new BanChatUserData(userId, eMessageType.All.getValue());
            data.setChannelId(channelId);
            data.setServerId(serverId);
            data.setHiddenTimeParam(System.currentTimeMillis() / 1000 + "-" + endTime / 1000);
            if (!banChatUserDataMap.containsKey(userId)) {
                Map<Integer, BanChatUserData> map = new HashMap<>();
                map.put(type.getType(), data);
                banChatUserDataMap.put(userId, map);
            } else if (!banChatUserDataMap.get(userId).containsKey(type.getType())) {
                Map<Integer, BanChatUserData> dataMap = banChatUserDataMap.get(userId);
                dataMap.put(type.getType(), data);
            }
            if (!new ChatControlDaoImpl().addBanChatUserData(data)) {
                return AdminResult.failMsg("新增禁言用户信息失败");
            }

        } else {
            BanChatUserData data = banChatUserDataMap.get(userId).get(type.getType());
            data.setHiddenTimeParam(System.currentTimeMillis() / 1000 + "-" + endTime / 1000);
            if (!new ChatControlDaoImpl().updateBanChatUserData(data)) {
                return AdminResult.failMsg("更新禁言用户信息失败");
            }
        }

        return AdminResult.defaultInstance();
    }

    /**
     * 发送封禁邮件
     */
    public void sendMail() {
        GamePlayer player = GamePlayerMgr.getPlayer(userId);
        if (player == null) {
            return;
        }
        String language = player.getLanguage();
        List<String> mail = mailMap.get(language);
        if (mail == null) {
            return;
        }
        MailManager.sendMail(player.getUserId(), eMailType.SYSTEM.getValue(), "", mail.get(1), mail.get(0));
    }

    /**
     * 获取封禁实例
     *
     * @param status  封禁类型
     * @param userId  用户ID
     * @param endTime 封禁结束时间
     * @return BanBase
     */
    public static BanBase getBanInstance(String status, long userId, long endTime) {
        ePlatformForbiddenType ePlatformForbiddenType = forName(status);
        if (ePlatformForbiddenType == null) {
            return null;
        }

        type = ePlatformForbiddenType;

        switch (ePlatformForbiddenType) {
            case BANNED:
                return new Banned(userId, endTime);
            case BAN_SPEAK:
                return new BanSpeak(userId, endTime);
            case DISABLE_EDIT_NAME:
                return new DisableEditName(userId, endTime);
        }

        return null;
    }

    /**
     * 通用获取封禁结果
     *
     * @return 封禁结果
     */
    public AdminResult getBanResult() {
        GamePlayer player = GamePlayerMgr.getPlayer(userId);
        if (player == null) {
            return AdminResult.failMsg("玩家不存在");
        }
        UserInfo userInfo = player.getUserInfo();
        if (userInfo == null) {
            return AdminResult.failMsg("玩家信息未找到");
        }
        String platformUserId = userInfo.getPlatformUserId();

        PlatformForbidden platformForbidden = ForbiddenMgr.getPlatformForbidden(platformUserId, type.getType());
        if (platformForbidden == null) {
            platformForbidden = new PlatformForbidden(platformUserId, type.getType(), System.currentTimeMillis(), endTime, 1);
            if (!PlatformUserIdInfoBussiness.addForbiddenInfo(platformForbidden)) {
                return AdminResult.failMsg("添加封禁状态记录失败");
            }
            ForbiddenMgr.addPlatformForbidden(platformForbidden);
        } else {
            platformForbidden.setForbiddenStartTime(System.currentTimeMillis());
            platformForbidden.setForbiddenTime(endTime);
            platformForbidden.setTimes(platformForbidden.getTimes() + 1);
            if (!PlatformUserIdInfoBussiness.updateForbiddenInfo(platformForbidden)) {
                return AdminResult.failMsg("更新封禁状态失败");
            }
        }

        return null;
    }

    /**
     * 通用获取解禁结果
     *
     * @return 解禁结果
     */
    public AdminResult getUnbanResult() {
        GamePlayer player = GamePlayerMgr.getPlayer(userId);
        if (player == null) {
            return AdminResult.failMsg("玩家不存在");
        }
        UserInfo userInfo = player.getUserInfo();
        if (userInfo == null) {
            return AdminResult.failMsg("玩家信息未找到");
        }
        String platformUserId = userInfo.getPlatformUserId();
        PlatformForbidden platformForbidden = ForbiddenMgr.getPlatformForbidden(platformUserId, type.getType());
        if (platformForbidden == null) { // 肯定先有封禁才有解封
            return AdminResult.failMsg("玩家" + type.getDesc() + "信息未找到");
        }
        platformForbidden.setForbiddenTime(0L);
        if (!PlatformUserIdInfoBussiness.updateForbiddenInfo(platformForbidden)) {
            return AdminResult.failMsg("更新封禁状态失败");
        }

        return null;
    }

    /**
     * 设置邮件模板
     *
     * @param mailMap 邮件模板
     */
    public void setMailMap(Map<String, List<String>> mailMap) {
        this.mailMap = mailMap;
    }
}
